crud operation in asp.net

64

crud operation in asp.net -

<section id="main-content">
<section>
<div>
<div>
<section>
<header>
<div>
<h1>Student Registration</h1>
</div>
</header>
<div>
<div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtStdname"><b>Student Name</b></asp:Label><br />
<asp:TextBox runat="server" required="required" Enabled="True" name="BrandName" ID="txtStdname" placeholder="Student Name"></asp:TextBox>
</div>
</div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtfname"><b>Father Name</b></asp:Label><br />
<asp:TextBox runat="server" required="required" Enabled="True" name="BrandName" ID="txtfname" placeholder="Father Name"></asp:TextBox>
</div>
</div>
</div>
<div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtdob"><b>DOB</b></asp:Label><br />
<asp:TextBox runat="server" required="required" TextMode="Date" Enabled="True" name="BrandName" ID="txtdob" placeholder="DOB "></asp:TextBox>
</div>
</div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtprogram" ><b>Program</b></asp:Label><br />
<asp:TextBox runat="server" required="required" Enabled="True" name="BrandName" ID="txtprogram" placeholder="Program"></asp:TextBox>
</div>
</div>
</div>
<div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtregion"><b>Region</b></asp:Label><br />
<asp:DropDownList ID="txtregion" CssClass="form-control input-sm" runat="server" >
<asp:ListItem Text="Pakistan" />
<asp:ListItem Text="Iran" />
<asp:ListItem Text="Iraq" />
<asp:ListItem Text="Turkey" />
<asp:ListItem Text="India" />
<asp:ListItem Text="China" />
</asp:DropDownList>
</div>
</div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtaddress"><b>Address</b></asp:Label><br />
<asp:TextBox runat="server" required="required" Enabled="True" name="BrandName" ID="txtaddress" placeholder=" Address "></asp:TextBox>
</div>
</div>
</div>
<div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtcell"><b>Cell No</b></asp:Label><br />
<asp:TextBox runat="server" required="required" TextMode="Number" Enabled="True" name="BrandName" ID="txtcell" placeholder="Cell Number "></asp:TextBox>
</div>
</div>
<div>
<div>
<asp:Label runat="server" AssociatedControlID="txtgender" ><b>Gender</b></asp:Label><br />
<asp:RadioButtonList CssClass="form-group" ID="txtgender" runat="server">
<asp:ListItem Text="Male" />
<asp:ListItem Text="Female" />
</asp:RadioButtonList>
</div>
</div>
</div>
<div>
<div>
<div>
<asp:Button Text="Save" ID="btnsave" OnClick="btnsave_Click" CssClass="btn btn-primary btn-lg" Width="220px" runat="server" />
<asp:Button Text="Update" ID="btnupdate" OnClick="btnupdate_Click" CssClass="btn btn-primary btn-lg" Width="220px" runat="server" />
<asp:Button Text="Delete" ID="btndlt" OnClick="btndlt_Click" CssClass="btn btn-danger btn-lg" Width="220px" runat="server" />
<asp:Label Text="" ForeColor="Green" Font-Bold="true" ID="lblmessage" CssClass="label label-success" runat="server" />
</div>
</div>
</div>
<div>
<div>
<div>
<div>
<asp:GridView ID="gv" Width="100%" AutoGenerateSelectButton="true" SelectedRowStyle-BackColor="Green" OnSelectedIndexChanged="gv_SelectedIndexChanged" runat="server"></asp:GridView>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</section>
</section>
Copy

crud operation in asp.net -

using System;
using System.Collections.Generic;
using Devart.Data.PostgreSql;
using System.Data;
using System.Web.UI.WebControls;
using System.Configuration;
namespace Projectsms
{
    class datalayer
    {
        PgSqlConnection conn_;
        PgSqlCommand cmd_;
        PgSqlDataReader reader_;
        PgSqlDataAdapter adptr_;
        System.Data.DataSet dset_;
        System.Data.DataTable dt_;
        static string getmessage { get; set; }
        public datalayer()
        {
            string cs = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
            conn_ = new PgSqlConnection(cs);
            cmd_ = new PgSqlCommand();
            adptr_ = new PgSqlDataAdapter();
            dset_ = new System.Data.DataSet();
        }
        public bool Connect()
        {
            try
            {
                conn_.Open();
                getmessage = "Connection established!";
                return true;
            }
            catch (Exception exp)
            {
                getmessage = "error while opening connection (Datalayer=>Connect()) : " + exp.Message;
                return false;
            }
        }
        public bool Disconnect()
        {
            try
            {
                conn_.Close();
                getmessage = "Connection Closed Successfully!";
                return true;
            }
            catch (Exception exp)
            {
                getmessage = "error while Closing connection (Datalayer=>Disconnect()) : " + exp.Message;
                return false;
            }
        }
        public string insertUpdateCreateOrDelete(string query)
        {
            string ret = "";
            string allqueries = query.ToLower();
            try
            {
                cmd_.CommandText = query;
                cmd_.Connection = conn_;
                Connect();
                cmd_.ExecuteNonQuery();
                if (allqueries.Contains("insert into "))
                {
                    ret = getmessage = "inserted Successfully!";
                }
                else if (allqueries.Contains("delete from "))
                {
                    ret = getmessage = "Deleted Successfully!";
                }
                else if (allqueries.Contains("create table "))
                {
                    ret = getmessage = "Table Created Successfully!";
                }
                else if (allqueries.Contains("update ") && allqueries.Contains("set= "))
                {
                    ret = getmessage = "Updated Successfully";
                }
            }
            catch (Exception exp)
            {
                ret = getmessage = "Failed to execute " + query + " n Reason : " + exp.Message;
            }
            finally { Disconnect(); }
            return ret;
        }
        public string fillgridView(string query, System.Web.UI.WebControls.GridView dgv)
        {
            dt_ = new System.Data.DataTable();
            string stret;
            try
            {
                cmd_.Connection = conn_;
                cmd_.CommandText = query.ToLower();
                Connect();
                adptr_.SelectCommand = cmd_;
                adptr_.Fill(dt_);
                dgv.DataSource = dt_;
                dgv.DataBind();
                stret = "Code Executed Successfully (filldatagridView()=> datalayer.cs)";
            }
            catch (Exception exp)
            {
                stret = "Failed (filldatagridView()=> datalayer.cs) : " + exp.Message;
            }
            finally
            {
                Disconnect();
                dt_ = null;
            }
            return stret;
        } 
    }
}
Copy

Comments

Submit
0 Comments